﻿using System;
using System.Net;
using System.Text;

namespace Songhay.Net
{
    /// <summary>
    /// Static members for <see cref="System.Net.WebRequest"/>.
    /// </summary>
    public static partial class HttpVerbAsyncUtility
    {
        /// <summary>
        /// Gets the specified resource as a string.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="getCompleteAction">The get complete action.</param>
        public static void Get(Uri resourceIndicator, Action<object, DownloadStringCompletedEventArgs> getCompleteAction)
        {
            if(getCompleteAction == null) throw new ArgumentNullException("getCompleteAction", "The required GET-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.DownloadStringCompleted += (o, args) =>
            {
                try
                {
                    getCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.DownloadStringAsync(resourceIndicator);
        }

        /// <summary>
        /// Gets the specified resource as a stream.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="getCompleteAction">The get complete action.</param>
        public static void GetStream(Uri resourceIndicator, Action<object, OpenReadCompletedEventArgs> getCompleteAction)
        {
            if(getCompleteAction == null) throw new ArgumentNullException("getCompleteAction", "The required GET-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.OpenReadCompleted += (o, args) =>
            {
                try
                {
                    getCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.OpenReadAsync(resourceIndicator);
        }

        /// <summary>
        /// Posts the specified resource.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="postCompleteAction">The post complete action.</param>
        public static void Post(Uri resourceIndicator, string postData, Action<object, UploadStringCompletedEventArgs> postCompleteAction)
        {
            if(postCompleteAction == null) throw new ArgumentNullException("postCompleteAction", "The required POST-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.UploadStringCompleted += (o, args) =>
            {
                try
                {
                    postCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.UploadStringAsync(resourceIndicator, postData);
        }
    }
}
